package cn.edu.zime.tjh47;

import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

public class Class8 extends AppCompatActivity {
    private String[] arrItems = {"第2课 示例任务1", "第2课 示例任务2", "第4课 示例任务1", "第5课 示例任务1",};
    private String[] hobby = {"唱", "跳", "rap", "篮球", "music", "鸡", "你", "太美"};
    private boolean[] arrSelected = new boolean[]{true, false, true, false};
    private boolean[] hobSelected = new boolean[]{true, false, true, false, true, false, true, false};
    private TextView tv_hobby;
    private int index = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.class8);
        tv_hobby = findViewById(R.id.tv_hobby);
        ActionBar bar = getSupportActionBar();
        bar.setTitle("龙龙占领地球");
        bar.setIcon(R.drawable.as_logo);
        bar.setDisplayShowHomeEnabled(true);
        bar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.class8_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.about) {
            showAboutDialog();
            return true;
        } else if (item.getItemId() == R.id.list_item) {
            showDlgList();
            return true;
        } else if (item.getItemId() == R.id.radio) {
            showRadioItem();
            return true;
        } else if (item.getItemId() == R.id.multiple) {
            showMultipleItem();
            return true;
        } else if (item.getItemId() == R.id.hobby) {
            showHobbyItem();
            return true;
        } else if (item.getItemId() == android.R.id.home) {
            finish();
        } else if (item.getItemId() == R.id.self) {
            showDlgSelf();
            return true;
        } else if (item.getItemId() == R.id.mycard) {
            showMyCard();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showMyCard() {
        new AlertDialog.Builder(this)
                .setTitle("个人名片")
                .setView(R.layout.class3_card)
                .setPositiveButton("确定", null)
                .create()
                .show();
    }

    private void showDlgSelf() {
        LayoutInflater inflater = getLayoutInflater();
        final View view = inflater.inflate(R.layout.class8_dlg, null);
        new AlertDialog.Builder(this)
                .setTitle("自定义对话框")
                .setView(view)
                .setPositiveButton("确定", (dialogInterface, i) -> {
                    EditText etUser = view.findViewById(R.id.et_user);
                    EditText etPwd = view.findViewById(R.id.et_pwd);
                    EditText etPhone = view.findViewById(R.id.et_phone);
                    String str = String.format("输入的数据为：\n用户名：%s\n密码：%s\n手机号：%s\n",
                            etUser.getText().toString(), etPwd.getText().toString(), etPhone.getText().toString());
                    Log.d("Demo8_1", str);
                    tv_hobby.setText(str);
                })
                .create()
                .show();
    }

    private void showHobbyItem() {
        new AlertDialog.Builder(this)
                .setTitle("爱好")
                .setMultiChoiceItems(hobby, hobSelected, (dialogInterface, i, b) ->
                        hobSelected[i] = b)
                .setPositiveButton("确定", (dialogInterface, i) -> {
                    StringBuilder str = new StringBuilder("你选择了:");
                    for (int j = 0; j < hobSelected.length; j++) {
                        if (hobSelected[j]) {
                            str.append(hobby[j]).append("\n");
                        }
                    }
                    tv_hobby.setText(str);
                })
                .create()
                .show();
    }

    private void showMultipleItem() {
        new AlertDialog.Builder(this)
                .setTitle("多选对话框")
                .setMultiChoiceItems(arrItems, arrSelected, (dialogInterface, i, b) ->
                        arrSelected[i] = b)
                .setPositiveButton("确定", (dialogInterface, i) -> {
                    String str = "选择了：";
                    for (int j = 0; j < arrSelected.length; j++) {
                        if (arrSelected[j]) {
                            str += arrItems[j] + ";";
                        }
                    }
                    Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
                })
                .create()
                .show();
    }

    private void showRadioItem() {
        new AlertDialog.Builder(this)
                .setTitle("单选对话框")
                .setSingleChoiceItems(arrItems, index, (dialogInterface, i) -> index = i)
                .setPositiveButton("确定", (dialogInterface, i) -> Toast.makeText(Class8.this, "选择了：" + arrItems[index], Toast.LENGTH_SHORT).show())
                .create()
                .show();
    }

    private void showDlgList() {
        new AlertDialog.Builder(this)
                .setTitle("列表对话框")
                .setItems(arrItems, (dialogInterface, i) ->
                        Toast.makeText(Class8.this, "选择了：" + arrItems[i], Toast.LENGTH_SHORT).show())
                .setPositiveButton("确定", null)
                .create()
                .show();

    }

    private void showAboutDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("关于");
        builder.setMessage("我是童佳豪");
        builder.setPositiveButton("确定", null);
        builder.show();
    }

    public void back(View view) {
        finish();
    }

    private void save(String username, String password) {

    }

}